package edu.msse.signup;

import java.sql.Time;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class TimeSlotService {
	 
	
	static public TimeSlot createTimeSlot(Time startTime, Time endTime, Position position, int numPositions)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            if(position!=null) 
        	{
            	position=jpa.merge(position);
        	}
            
            TimeSlot t=new TimeSlot(startTime, endTime, position, numPositions);
    		jpa.persist(t);
            tx.commit();
            t=jpa.merge(t);
            return t;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
        
	}


	public Users addUser(TimeSlot timeSlot, Users user)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            timeSlot=jpa.merge(timeSlot);
            if(timeSlot.getUsers().size() < timeSlot.getNumberOfVolunteers()){
            	 user=jpa.merge(user);
                 user.addTimeSlot(timeSlot);
                 timeSlot=jpa.merge(timeSlot);
                 timeSlot.addUser(user);
                 user=jpa.merge(user);
                 jpa.persist(timeSlot); 	
            }
            else throw new Exception("Timeslot Already Filled");
            tx.commit();
            return user;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
    }
	
	static TimeSlot update(TimeSlot timeSlot)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            timeSlot=jpa.merge(timeSlot);
            jpa.persist(timeSlot);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
		return timeSlot;
	}
	
	static Boolean delete(TimeSlot timeSlot)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            timeSlot=jpa.merge(timeSlot);
            jpa.remove(timeSlot);
            tx.commit();
            return true;
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return false;
            
        }
		
	}
}
